
<?php
class TconfController extends AppController {


	var $name = 'Tconf';
	var $components = array('Xacl');

	/**
	 * all actions list
	 *
	 */
	function index()
	{

		$this->paginate = array('fields'=>array('Tconf.id', 'Tconf.f_key', 'Tconf.f_desc'), 'limit'=>10, 'order'=>'Tconf.id desc');

		$this->loadModel("Tconf");
		$this->set('conflist', $this->paginate($this->Tconf));
		$this->layout = 'tuser';
	}

    /**
     * new and save a system variable
     */
    function newvar()
    {
        if (! empty($_POST)) {
			$data = array('f_key'=>isset($_POST["fkey"]) ? trim($_POST["fkey"]) : '', 'f_value'=>isset($_POST["fvalue"]) ? trim($_POST["fvalue"]) : '',
                          'f_desc'=>isset($_POST["fdesc"]) ? trim($_POST["fdesc"]) : '');

			if ( empty($data['f_key'])) {
				$this->flash("变量Key不能为空!", "/tconf/newvar");
				return;
			}

			if ( empty($data['f_value'])) {
				$this->flash("变量Value不能为空!", "/tconf/newvar");
				return;
			}
			if (false != $this->Tconf->findByKey($data['f_key'])) {
				$this->flash("变量Key被占用!", "/tconf/newvar");
				return;
			}
			if ($this->Tconf->save($data)) {
                $confVar = $this->Tconf->find('all', array('fields'=>array('f_key','f_value')));
                if(!empty($confVar)){
                    $sysconfig = array();
                    foreach($confVar as $conf)
                    {
                        $key = $conf['Tconf']['f_key'];
                        $value = $conf['Tconf']['f_value'];
                        $sysconfig[$key] = $value;
                    }
                    Cache::write('sysytemconfig',$sysconfig);
                    Cache::set(array('duration' => '+30 days'));
                }
				$this->flash("添加成功!", "/tconf");
				return;
			} else {
				$this->flash("添加失败!", "/tconf/newvar");
				return;
			}
		} else {
            $this->layout = 'tuser';
        }
    }
    
    /**
	 * delete variable
	 *
	 * @param int $kid variable id
	 * @return
	 */
	function delvar($id=NULL)
	{
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$this->Tconf->deleteAll("id='$id'");

		$this->flash('操作成功!', '/tconf');
		return '';
	}

    /**
     * edit the variable
     *
     * * @param int $kid variable id
	 * @return
     */
    function editvar($id=NULL)
    {
        if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
        $conf = $this->Tconf->read(null, $id);
        $this->set('conf', $conf['Tconf']);
        $this->layout = 'tuser';
    }

    /*
     * 保存变量更新
     */
    function updatevar()
    {
       if (! empty($_POST)) {
			$data = array('id'=>$_POST['id'],
                           'f_value'=>isset($_POST["f_value"]) ? trim($_POST["f_value"]) : '',
                          'f_desc'=>isset($_POST["f_desc"]) ? trim($_POST["f_desc"]) : '');
            if($this->Tconf->save($data)){
                $confVar = $this->Tconf->find('all', array('fields'=>array('f_key','f_value')));
                if(!empty($confVar)){
                    $sysconfig = array();
                    foreach($confVar as $conf)
                    {
                        $key = $conf['Tconf']['f_key'];
                        $value = $conf['Tconf']['f_value'];
                        $sysconfig[$key] = $value;
                    }
                    Cache::write('sysytemconfig',$sysconfig);
                    Cache::set(array('duration' => '+30 days'));
                }
                $this->flash("修改成功!", "/tconf");
				return;
            }
       } else {
           echo '操作无效!';
		   return '';
       }
    }

}
?>
